package sr.codemg.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import sr.codemg.config.RedisUtil;
import sr.codemg.entity.SysParams;
import sr.codemg.mapper.SysParamsMapper;
import sr.codemg.service.SysParamsService;
import sr.codemg.utils.Constants;
import sr.codemg.utils.ResponseCode;
import sr.codemg.utils.StaticFunc;
import sr.codemg.vm.Base;
import sr.codemg.vm.Tables;

import java.util.*;

/**
 * 参数信息
 **/
@Service
public class SysParamsServiceImpl implements SysParamsService {

    @Autowired
    SysParamsMapper sysParamsMapper;

    @Autowired
    RedisUtil redisUtil;

    @Override
    public SysParams get(String id) {
        return sysParamsMapper.get(id);
    }

    @Override
    public Tables findList(Map<String, Object> params) {
        StaticFunc.page(params);
        List<SysParams> data = sysParamsMapper.findList(params);
        Integer count = sysParamsMapper.count(params);
        return new Tables(data, count);
    }

    @Override
    public Base.Response add(SysParams entity) {
        sysParamsMapper.add(entity);
        return ResponseCode.getBase(Constants.SUCC);
    }

    @Override
    public Base.Response update(SysParams entity) {
        sysParamsMapper.update(entity);
        return ResponseCode.getBase(Constants.SUCC);
    }

    @Override
    public Base.Response delete(String id) {
        sysParamsMapper.delete(id);
        return ResponseCode.getBase(Constants.SUCC);
    }

    @Override
    public Set<SysParams> select(String groupId, String in, String notIn) {
        Set<SysParams> list = (Set<SysParams>) redisUtil.hget(Constants.SYS_CACHE.SYS_PARAMS, groupId);
        if (list == null) {
            return null;
        }
        Iterator<SysParams> iterator = list.iterator();
        if (!StringUtils.isEmpty(in)) {
            String[] ins = in.split(",");
            while (iterator.hasNext()) {
                if (!Arrays.asList(ins).contains(iterator.next().getKey())) {
                    iterator.remove();
                }
            }
        } else if (!StringUtils.isEmpty(notIn)) {
            String[] notIns = notIn.split(",");
            while (iterator.hasNext()) {
                for (String v : notIns) {
                    if (iterator.next().getKey().equals(v)) {
                        iterator.remove();
                    }
                }
            }
        }
        return list;
    }

    @Override
    public void refresh() {
        redisUtil.del(Constants.SYS_CACHE.SYS_PARAMS);
        List<SysParams> params = sysParamsMapper.getAll();
        Set<SysParams> data;
        Set<String> cache = new HashSet();
        for (SysParams item : params) {
            if (cache.contains(item.getGroupId())) {
                continue;
            }
            data = new HashSet<>();
            for (SysParams sys : params) {
                if (item.getGroupId().equals(sys.getGroupId())) {
                    data.add(sys);
                }
            }
            redisUtil.hset(Constants.SYS_CACHE.SYS_PARAMS, item.getGroupId(), data);
            cache.add(item.getGroupId());
        }
    }
}

